// $Id: PackagesView.java 81 2012-01-04 22:16:48Z huijzer@gmail.com $
package lime.workflow.view;

import java.util.Collections;
import java.util.List;

import lime.model.Workitem;
import lime.model.WorkitemPackage;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.TableColumn;

public class PackagesView extends AbstractWorkitemListenerView {

	public static final String ID = "lime.workflow.view.packages";
	
	// Widgets
	private TableViewer viewer;
	
	@Override
	public void createPartControl(Composite parent) {
		
		viewer = new TableViewer(parent, SWT.FULL_SELECTION | SWT.BORDER);
		viewer.getTable().setHeaderVisible(true);
		viewer.getTable().setLinesVisible(true);

		TableViewerColumn nameColumn = new TableViewerColumn(viewer, SWT.NONE);
		nameColumn.getColumn().setText("Name");
		TableViewerColumn valueColumn = new TableViewerColumn(viewer, SWT.NONE);
		valueColumn.getColumn().setText("Type");
		TableViewerColumn componentIdColumn = new TableViewerColumn(viewer, SWT.NONE);
		componentIdColumn.getColumn().setText("Component ID");
		TableViewerColumn componentNameColumn = new TableViewerColumn(viewer, SWT.NONE);
		componentNameColumn.getColumn().setText("Component Name");

		viewer.setContentProvider(new WorkitemPackageProvider());
		viewer.setLabelProvider(new WorkitemPackageLabelProvider());

		// Make sure the columns take up as much space as needed, but no more.
		for (TableColumn column : viewer.getTable().getColumns()) {
			column.pack();
		}
		
		getSite().setSelectionProvider(viewer);
	}

	@Override
	public void setFocus() {
		viewer.getTable().setFocus();
	}

	protected void update(final Workitem workitem) {
		
		final Thread myThread = new Thread("MyThread") {
			public void run() {
				String workitemId = workitem.getObjectId();
				final List<WorkitemPackage> packages = getWorkflowService().getWorkitemPackages(workitemId);
				Collections.sort(packages);
				getSite().getShell().getDisplay().asyncExec(new Runnable() {
					public void run() {
						setContentDescription(workitem.getProcessName() + " :: " + workitem.getActivityName());
						viewer.setInput(packages);
						// Make sure the columns take up as much space as
						// needed, but no more.
						for (TableColumn column : viewer.getTable().getColumns()) {
							column.pack();
						}
						viewer.refresh();
					}
				});
			}
		};
		myThread.start();
	}

	private class WorkitemPackageProvider implements IStructuredContentProvider {
		
		@SuppressWarnings("unchecked")
		@Override
		public Object[] getElements(Object element) {
			List<WorkitemPackage> packages = (List<WorkitemPackage>) element;
			return packages.toArray();
		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub
			
		}
	}

	private class WorkitemPackageLabelProvider implements ITableLabelProvider {

		@Override
		public void addListener(ILabelProviderListener listener) {
			// TODO Auto-generated method stub

		}

		@Override
		public void dispose() {
			// TODO Auto-generated method stub

		}

		@Override
		public boolean isLabelProperty(Object element, String property) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void removeListener(ILabelProviderListener listener) {
			// TODO Auto-generated method stub

		}

		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public String getColumnText(Object element, int columnIndex) {
			WorkitemPackage workitemPackage = (WorkitemPackage) element;
			String text = "";

			switch (columnIndex) {
				case 0:
					text = workitemPackage.getName();
					break;
				case 1:
					text = workitemPackage.getType();
					break;
				case 2:
					text = workitemPackage.getComponentId();
					break;
				case 3:
					text = workitemPackage.getComponentName();
					break;
				default:
					break;
			}

			return text;
		}
	}
	
}
